﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VSDBM.Code.functions;

public partial class Visual_CRUD_Sequence_ImportSequence : System.Web.UI.Page
{
    public String IdReferencia;
    public int qntFound;
    public String organismName;



    protected void Page_Load(object sender, EventArgs e)
    {
        verificaUser();
    }

    protected void verificaUser()
    {
        VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
        if (!(usuariologado.acessgroup == 1 || usuariologado.acessgroup == 2))
        {
            HttpContext.Current.Session["exception"] = "You are not authorized!";
            Response.Redirect("~/Visual/Errors/error.aspx");
        }
    }
    public void doPostPasso1()
    {
        btnImportFromFile.Visible = false;
        this.IdReferencia = txtIdReferencia.Text;
        try
        {
            this.organismName = zeseeker.getOrganism(IdReferencia, "nuccore");
            this.qntFound = zeseeker.getQnt(this.organismName);
            lblErroNotFound.Visible = false;
        }
        catch (Exception e)
        {
            if (e.Message == "O servidor remoto retornou um erro: (400) Solicitação Incorreta.")
            {
                lblErroNotFound.Visible = true;
            }
            else
            {
                HttpContext.Current.Session["exception"] = e.Message;
                Response.Redirect("../../../Visual/Errors/error.aspx");
            }
        }
        lblqntp2.Text = Convert.ToString(qntFound);
        p2lblOrganismo.Text = this.organismName;
    }
    public void doPostPasso2()
    {

    }

    protected void CheckStep(object sender, EventArgs e)
    {
        if (Wizard1.ActiveStep.Name == "Passo 1")
        {

        }
        else if (Wizard1.ActiveStep.Name == "Passo 2")
        {
            if (qntFound == 0)
            {
                Wizard1.MoveTo(Wizard1.WizardSteps[0]);
            }
            else
            {
                if (qntFound > 1000)
                {
                    btnExportId.Visible = true;
                    btnAutoImport.Visible = false;
                }
                else
                {
                    btnExportId.Visible = true;
                    btnAutoImport.Visible = true;
                }
            }

        }
    }

    protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        if (Wizard1.ActiveStep.Name == "Passo 1")
        {
            doPostPasso1();
        }
        else if (Wizard1.ActiveStep.Name == "Passo 2")
        {
            doPostPasso2();
        }
    }

    protected void btnAutoImport_Click(object sender, ImageClickEventArgs e)
    {
        p2lblDone.Visible = false;
        p2lblAguarde.Visible = true;
        VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
        zeseeker ze = new zeseeker();
        if (usuariologado == null)
        {
            HttpContext.Current.Session["exception"] = "No autorized user found!";
            Response.Redirect("../../../Visual/Errors/error.aspx");
        }
        try
        {
            ze.executeeSearch(txtIdReferencia.Text, p2lblOrganismo.Text, "nuccore", usuariologado.id);
        }
        catch (Exception affe)
        {

            HttpContext.Current.Session["exception"] = affe.Message;
            Response.Redirect("../../../Visual/Errors/error.aspx");
        }

        HttpContext.Current.Session["done"] = qntFound + "Sequences Added.";
        Response.Redirect("../../../Visual/Done/Done.aspx");
    }

    protected void btnExportId_Click1(object sender, ImageClickEventArgs e)
    {
        p2lblDone.Visible = false;
        p2lblAguarde.Visible = true;

        String IdList = zeseeker.GetIdList(p2lblOrganismo.Text, "nuccore", Convert.ToInt32(lblqntp2.Text));
        String FileName = p2lblOrganismo.Text.Replace(" ", "");
        Response.ContentType = "text/plain";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".txt");
        Response.Write(IdList);
        Response.End();

        HttpContext.Current.Session["done"] = "ID file generated.";
        Response.Redirect("../../../Visual/Done/Done.aspx");
    }

    protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        Response.Redirect("ImportSequence.aspx");
    }

    protected void btnImportFromFile_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ImportFromFile.aspx");
    }

    protected void txtIdReferencia_TextChanged(object sender, EventArgs e)
    {
        btnImportFromFile.Visible = false;
    }

    protected void Wizard1_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
    {
        Response.Redirect("ImportSequence.aspx");
    }
}